<?php

echo $this->Html->css('jquery-ui/jquery.ui.all.css');
echo $this->Html->css('jquery-ui/jquery.ui.autocomplete.css');

echo $this->Html->script('bib/jquery-ui/jquery.ui.core.js');
echo $this->Html->script('bib/jquery-ui/jquery.ui.widget.js');
echo $this->Html->script('bib/jquery-ui/jquery.ui.button.js');
echo $this->Html->script('bib/jquery-ui/jquery.ui.position.js');
echo $this->Html->script('bib/jquery-ui/jquery.ui.autocomplete.js');

echo $this->Html->script('nav/switchNavigation');

?>

<style>
    .ui-autocomplete .ui-menu-item{
       background : #fff;
    }
</style>	

<?php

    $usersVideo = $video['UsersVideo'];
    
      /**
       * Créer les options du select pour la liste des figures
       */
    $options = array();
    foreach ($listTricks as $trickType => $multiples){
        foreach ($multiples as $multiple => $tricks){
            foreach ($tricks as $trickId => $trick){
               $options[$trickId] = $this->Display->trick($trickType, $multiple, $trick['spin']);
            }
        }
    }
    
    ?>
<h2>Edition : <?php echo $usersVideo['title'];?> </h2>

<table >
    <tbody>
        <tr>
            <td>Title</td>
            <td> <?php echo $usersVideo['title'];?></td>
        </tr>
        <tr>
            <td>Sport</td>
            <td> <?php echo $usersVideo['sports_id'];?></td>
        </tr>
        <tr>
            <td>Uploaded on </td>
            <td> <?php echo $usersVideo['posted_on'];?></td>
        </tr>
        <tr>
            <td> Type  </td>
            <td> 
            <?php 
            $usersVideosType = $video['UsersVideo']['type'];
            if ($usersVideosType == 'null'){
                $usersVideosType = false;
            }
            echo $usersVideosType;
            /*
            if ($usersVideosType){
                echo 'Montage';
                $usersVideosType = 'VideosEditing';
            }
            else if ($video['VideosTrick']){
                echo 'Figure';
                $usersVideosType = 'VideosTrick';
            }
            else if ($video['VideosTutorial']){
                echo 'Tutorial';
                $usersVideosType= 'VideosTutorial';
            }
            else{
                echo 'Vidéo non publié';
                $usersVideosType = false;
            }
             * 
             */
            ?>
            </td>
        </tr>
    </tbody>
</table>


<?php

if (!$usersVideosType){
?>

<h3>Publier la vidéo en tant que </h3>
<div id="switchBox">
    <div id="linkSwitchBox">
        
    </div>
    
    <div class="switchNavigation">
        <div class="navSwitch">
            
        </div>
        <div id="tutorialForm" class="item">
<?php
}

if (!$usersVideosType ||  $usersVideosType == 'VideosTutorial'){
?>
            <h3 class="title">Vidéo tutoriel</h3>
            <?php
            echo $this->Form->create('VideosTutorial');

            echo $this->Form->input('VideosTutorial.users_videos_id');
            
            echo $this->Form->input('title', array(
                'label' => 'Title : ',
                'type' => 'text'
            ));

            echo $this->Form->input('description', array(
                'label' => 'Description : ',
                'type' => 'textarea'
            ));

            echo $this->Form->input('modules_sports_id', array(
                'label' => 'Module : ',
                'options' => $listModules
            ));

              ?>
            <div class="ui-widget">
            <?php
            echo $this->Form->input('tricks_id', array(
                'options' => $options,
                'id' => 'combobox'
            ))
            ?>
            </div>

            <?php

            echo $this->Form->end('Save');
            ?>
<?php
}
if (!$usersVideosType){
?>
            
        </div>
        <div id="uniqueTrickForm"  class="item">
            <h3 class="title">Unique trick</h3>

<?php
}

if (!$usersVideosType ||  $usersVideosType == 'VideosTrick'){

            echo $this->Form->create('VideosTrick');

            echo $this->Form->input('VideosTrick.users_videos_id');
            
            echo $this->Form->input('modules_sports_id', array(
                'label' => 'Module : ',
                'options' => $listModules
            ));

              ?>
            <div class="ui-widget">
            <?php
            echo $this->Form->input('tricks_id', array(
                'options' => $options,
                'id' => 'combobox'
            ))
            ?>
            </div>

            <?php

            echo $this->Form->end('Save');
            ?>


<?php
}

if (!$usersVideosType){
?>
        </div>
        <div id="editingForm"  class="item">
            <h3 class="title">Montage vidéo</h3>
<?php
}

if (!$usersVideosType ||  $usersVideosType == 'VideosEdition'){


            echo $this->Form->create('VideosEditing');

            echo $this->Form->input('title', array(
                'label' => 'Title : ',
                'type' => 'text'
            ));

            echo $this->Form->input('description', array(
                'label' => 'Description : ',
                'type' => 'textarea'
            ));

            echo $this->Form->input('riders', array(
                'label' => 'Riders : '
            ));

            echo $this->Form->input('tag', array(
                'label' => 'Tag : '
            ));

            echo $this->Form->end('Save');

}
if (!$usersVideosType){
            ?>
        </div>    
    </div>
</div>
<?php
}
?>
    <div class="clear">
<?php

    debug($video);
?>
        
    </div>










     
	<script>
	(function( $ ) {
		$.widget( "ui.combobox", {
			_create: function() {
				var self = this,
					select = this.element.hide(),
					selected = select.children( ":selected" ),
					value = selected.val() ? selected.text() : "";
				var input = this.input = $( "<input>" )
					.insertAfter( select )
					.val( value )
					.autocomplete({
						delay: 0,
						minLength: 0,
						source: function( request, response ) {
							var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
							response( select.children( "option" ).map(function() {
								var text = $( this ).text();
								if ( this.value && ( !request.term || matcher.test(text) ) )
									return {
										label: text.replace(
											new RegExp(
												"(?![^&;]+;)(?!<[^<>]*)(" +
												$.ui.autocomplete.escapeRegex(request.term) +
												")(?![^<>]*>)(?![^&;]+;)", "gi"
											), "<strong>$1</strong>" ),
										value: text,
										option: this
									};
							}) );
						},
						select: function( event, ui ) {
							ui.item.option.selected = true;
							self._trigger( "selected", event, {
								item: ui.item.option
							});
						},
						change: function( event, ui ) {
							if ( !ui.item ) {
								var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
									valid = false;
								select.children( "option" ).each(function() {
									if ( $( this ).text().match( matcher ) ) {
										this.selected = valid = true;
										return false;
									}
								});
								if ( !valid ) {
									// remove invalid value, as it didn't match anything
									$( this ).val( "" );
									select.val( "" );
									input.data( "autocomplete" ).term = "";
									return false;
								}
							}
						}
					})
					.addClass( "ui-widget ui-widget-content ui-corner-left" );

				input.data( "autocomplete" )._renderItem = function( ul, item ) {
					return $( "<li></li>" )
						.data( "item.autocomplete", item )
						.append( "<a>" + item.label + "</a>" )
						.appendTo( ul );
				};

				this.button = $( "<button type='button'>&nbsp;</button>" )
					.attr( "tabIndex", -1 )
					.attr( "title", "Show All Items" )
					.insertAfter( input )
					.button({
						icons: {
							primary: "ui-icon-triangle-1-s"
						},
						text: false
					})
					.removeClass( "ui-corner-all" )
					.addClass( "ui-corner-right ui-button-icon" )
					.click(function() {
						// close if already visible
						if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
							input.autocomplete( "close" );
							return;
						}

						// work around a bug (likely same cause as #5265)
						$( this ).blur();

						// pass empty string as value to search for, displaying all results
						input.autocomplete( "search", "" );
						input.focus();
					});
			},

			destroy: function() {
				this.input.remove();
				this.button.remove();
				this.element.show();
				$.Widget.prototype.destroy.call( this );
			}
		});
	})( jQuery );

	$(function() {
		$( "#combobox" ).combobox();
		$( "#toggle" ).click(function() {
			$( "#combobox" ).toggle();
		});
	});
	</script>
            




 <script type="text/javascript">
                
                $(document).ready(function(){
                    var precSearch = ''; // La valeur précédente de la recherche
                    var baseUrl = '<?php echo $this->webroot;?>';
                    
                    $('#fastTrickSearch').keyup(function(){
                        var search = $(this).val();
                        
                        // Si on ajoute une lettre, on tri parmi les résultat que l'on a déja récupéré.
                        if (search.lenght > precSearch.length){
                            
                        }
                        // Sinon on relance une nouvelle recherche
                        
                        $.ajax({
                            url : baseUrl + 'Tricks/searchTricks',
                            data : {search : search, sports_id : 3},
                            success : function(data){
                                $('#rep').html(data);
                                
                            },
                            error : function(){
                                alert('sa merde');
                            },
                            type : 'POST'
                        });
                        
                    });
                    
                });
            </script>